package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.OmcSignInDto;
import com.csun.cmny.provider.dto.OmcSignInQueryDto;
import com.csun.cmny.provider.model.domain.OmcSignIn;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcSignInService;
import com.csun.cmny.provider.vo.OmcSignInVo;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author lijun
 * @description:
 * @date 2019/11/27 13:49
 */
@Slf4j
@RestController
@RequestMapping(value ="/signIn", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcSignInContorller extends BaseController {
    @Resource
    private OmcSignInService omcSignInService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 保存记录
     * @param signInDto
     * @return
     */
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/addSignIn")
    public Wrapper addSignIn(@RequestBody OmcSignInDto signInDto){
        signInDto.setGroupId(getLoginAuthDto().getGroupId());
        Date date = new Date();
        signInDto.setSigninTime(date);
        Integer result = save(signInDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null),result);
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }
    public Integer save(OmcSignInDto signInDto) {
        OmcSignIn omcsignIn = new OmcSignIn();
        BeanUtils.copyProperties(signInDto, omcsignIn);
        return omcSignInService.save(omcsignIn);
    }

    /**
     * 查询打卡记录
     * @param signInDto
     * @return
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/querySignInWithPage", method = RequestMethod.POST)
    public Wrapper querySignInWithPage (@RequestBody OmcSignInQueryDto signInDto){
        signInDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = omcSignInService.querySignInWithPage(signInDto);
        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),pageInfo);
    }

    /**
     * 查询该设备七天内的打卡记录
     * @param deviceId
     * @return
     */
  //  @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/querySignIn/{deviceId}", method = RequestMethod.POST)
    public Wrapper querySignIn (@PathVariable Long deviceId){
        List<OmcSignInVo> list = omcSignInService.querySignIn(deviceId);
        //return WrapMapper.ok(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),list);
    }
}
